<!doctype html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/header.css" rel="stylesheet" />
	</head>
	<body>
		<header class="mui-bar mui-bar-nav title">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color: white;"></a>
			<a id="up_nickname" class="mui-icon mui-icon-right-nav mui-pull-right" style="color: white;line-height: 26px;font-size: 14px;">完成</a>
			<h1 class="mui-title title-color">设置昵称</h1>
		</header>
		
		<div class="mui-content">
		     <div class="mui-input-row" style="margin: 10px;">
		         <input id="text_nickname" type="text" class="mui-input-speech mui-input-clear" placeholder="语音输入昵称">
		     </div>
		</div>
	
		<script src="js/mui.min.js"></script>
		<script src="js/app.js" type="text/javascript" ></script>
		<script type="text/javascript">
			mui.init({
				swipeBack:true //启用右滑关闭功能
			});
			mui.plusReady(function() {
				var fr_user_info = app.getUserInfo();
				var nickName = fr_user_info.nickName;
				var text_nickname = document.getElementById("text_nickname");
				text_nickname.value = nickName;    
			    
			    var up_nickname = document.getElementById("up_nickname");
			    up_nickname.addEventListener("tap",function(){
			    		var inputName = text_nickname.value;
					var inputName = inputName.replace(/\s*/g,"");
			    		if(inputName.length>12 || inputName.length<5){
			    			app.showToast("昵称长度5-12位！","error");
						return false;
			    		}
			    		if(inputName == nickName){
			    			mui.back();
			    			return false;
			    		}
			    		plus.nativeUI.showWaiting("请稍等");
			    		mui.ajax(app.serverUrl + '/u/setNickName',{
						data:{
							nickName: inputName,
							userId: fr_user_info.userId,
						},
						dataType:'json',//服务器返回json格式数据
						type:'post',//HTTP请求类型
						timeout:10000,//超时时间设置为10秒；
						headers:{'Content-Type':'application/json'},	              
						success:function(data){
							plus.nativeUI.closeWaiting();
							app.showToast(data.msg, "success");
							if(data.status == 200){
								var userInfo = data.data;
								app.setUserInfo(userInfo);
								// 触发另外一个webview的自定义事件，可以使用 mui.fire()
								var meWebview = plus.webview.getWebviewById("me_setting.html");
								mui.fire(meWebview, "refresh");
								mui.back();
							}else{
								app.showToast(data.msg, "error");	
							}
						},
						error:function(xhr,type,errorThrown){
							app.Error();
						}
					});
			    });
 				
			})
		</script>
	</body>
</html>
